/**
 * 将文档到处为js对象，用于生成测试用例
 */
(function a() {
  const result = {};
  const tbodies = document.querySelectorAll("table tbody");
  tbodies.forEach((tbody) => {
    const rows = tbody.querySelectorAll("tr");
    rows.forEach((row) => {
      const tdCount = row.querySelectorAll("td").length;
      let codePositioin = 3;
      let effectPosition = 5;
      if (tdCount === 6) {
        codePositioin = 4;
        effectPosition = 6;
      }
      const thirdColumnCell = row.querySelector(`td:nth-child(${codePositioin}) code`);
      if (thirdColumnCell) {
        const permision = thirdColumnCell.textContent.trim();
        const pElement = row.querySelectorAll(`td:nth-child(${effectPosition}) p`);
        if (pElement.length > 0) {
          pElement.forEach((p) => {
            const p_ = p.cloneNode(true);
            const codeElements = p_?.querySelectorAll("code");
            codeElements?.forEach((codeElement) => {
              const textContent = codeElement.textContent;
              const newTextNode = document.createTextNode(`\`${textContent}\``);
              codeElement.replaceWith(newTextNode);
            });
            const behavior = p_.textContent.trim();
            result[permision] = result[permision]
              ? [...result[permision], behavior]
              : [behavior];

            const academicIndex = result[permision].findIndex(
              (i) => i === "Academic"
            );
            if (academicIndex > 0) {
              result[permision] = result[permision].slice(0, academicIndex);
            }
            result[permision] = result[permision].filter(i => !['', "CRM"].includes(i))
          });
        }
      }
    });
  });

  // 输出所有第三列的文本内容
  console.log(
    result,
    'permission count: ' + Object.keys(result).length,
    'effect count: ' + Object.values(result).reduce((memo, item) => memo + item.length, 0)
  );
  window.temp1 = result;
})();
